package gbench.appdemo.mall.erp.report;

import org.junit.jupiter.api.Test;

import gbench.common.fs.FileSystem;
import gbench.common.matlib.MatlibCanvas.XVec;
import gbench.common.matlib.data.DataReader.DFrame;
import gbench.common.matlib.rdd.BasePair;
import gbench.common.tree.LittleTree.Term;
import static gbench.common.fs.XlsFile.*;
import static gbench.common.matlib.MatlibCanvas.println;
import static gbench.common.tree.LittleTree.IRecord.REC;

/**
 * 
 * @author gbench
 *
 */
public class MyReport extends ReportEngine{
    
    public MyReport(){
        super(4);
    }

    @Test
    public void foo() {
        final var file = FileSystem.path("中国国际收支平衡表时间序列(BPM6).xlsx", getClass());
        final var excel = new SimpleExcel(file); // 获取 excel 文件
        final var dfm = excel.collect(0,DFrame.dfmclc2); // 生成数据列表
        final var hh = dfm.row(2, r -> r.arrayOf(String::toString, XVec::OF).list()); // header
        final var lineS = dfm.rowsS(e -> BasePair.bp(e.str(0), e)).filter(p -> !p._1().equals("null")); // 去除掉空值行
        
        yml(lineS.peek(e -> println(e._2())), (node, u) -> node.setAttribute("props", u.rename(hh))).filter(e -> e.getAttributes().size() > 0)
        .forEach(e -> {
            final var x = e.pattr2dbl("props/2010");
            println(e.getPathCode(), Term.FT("$0 \t $1 \t $2", "".repeat(e.getLevel()), e.token("/"), x));
            final var debit = e.childOf("借方");
            final var credit = e.childOf("贷方");
            if (debit != null && credit != null) { // 处理借贷
                final var dr = debit.pattr2dbl("props/2010");
                final var cr = credit.pattr2dbl("props/2010");
                if (dr != null && cr != null) {
                    println(e.token("/"), "---------------->", "借贷分录", REC("dr", dr, "cr", cr, "balance", dr + cr));
                } // if
            } // if
        });
        
        excel.close();
    }
    
}
